import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron/simple'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
import { getEnvConfig } from './config'

const __dirname = dirname(fileURLToPath(import.meta.url))
const envConfig = getEnvConfig(process.env.NODE_ENV)

// https://vite.dev/config/
export default defineConfig({
  build: {
    outDir: 'dist-web',
    emptyOutDir: true,
  },
  define: {
    VITE_CONFIG: JSON.stringify(envConfig),
  },
  resolve: {
    alias: {
      '@web': resolve(__dirname, 'src/web'),
      '@app': resolve(__dirname, 'src/app'),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          @forward "@web/asset/css/common/_config.scss" with ($elNamespace: "${envConfig.elNamespace}", $appNamespace: "${envConfig.appName}");
          @use "@web/asset/css/common/_var.scss"as *;
        `,
      },
    },
  },
  plugins: [
    vue(),
    electron({
      main: {
        entry: resolve(__dirname, 'src/app/main.ts'),
        vite: {
          define: {
            VITE_CONFIG: JSON.stringify(envConfig),
          },
        },
      },
      preload: {
        input: resolve(__dirname, 'src/app/preload/preload.ts'),
      },
    }),
  ],
})
